class Solution:
    def findLength(self, A, B) -> int:
        maxLen = 0
        bhash = {}
        for i in range(len(B)):
            if B[i] not in bhash:
                bhash[B[i]] = set()
            bhash[B[i]].add(i)
        ahash = {}
        for i in range(len(A)):
            tmp = {}
            if A[i] in bhash:
                for j in bhash[A[i]]:
                    tmp[j+1] = 1
                    maxLen = max(maxLen, 1)
            for key in ahash:
                if key<len(B):
                    if A[i] == B[key]:
                        tmp[key + 1] = ahash[key] + 1
                        maxLen = max(maxLen, tmp[key + 1])
            ahash = tmp
        return maxLen

if __name__ == '__main__':
    so = Solution()
    A=[1, 2, 3, 2, 1]
    B=[3, 2, 1, 4, 7]
    r = so.findLength(A,B)
    print(r)
